﻿Imports DevExpress.XtraGrid.Views.Grid
Public Class frmWaitCodeItems
    Dim _Mysql As New ClsSQLhelper
    Dim Strsql As String
    Dim On_Load As Boolean = True
    Dim Primary_Code As Integer = -1
#Region "Function"
    Sub Load_Data()

        Try
            Me.GridColumn2.FieldName = "fullItemcode"
            Me.GridColumn2.Caption = "หมายเลขครุภัณฑ์"
            Me.GridColumn2.OptionsColumn.ReadOnly = True
            ' Me.GridColumn2.Visible = True
            Me.GridColumn12.FieldName = "Item_Name"
            Me.GridColumn12.Caption = "ชื่อครุภัณฑ์"
            Me.GridColumn12.OptionsColumn.ReadOnly = True
            'Me.GridColumn12.Visible = True
            Me.GridColumn13.FieldName = "Band"
            Me.GridColumn13.Caption = "ยี่ห้อ"
            Me.GridColumn13.OptionsColumn.ReadOnly = True
            'Me.GridColumn13.Visible = True
            Me.GridColumn14.FieldName = "Product_From_Country"
            Me.GridColumn14.Caption = "ผลิตภัณฑ์จากประเทศ"
            Me.GridColumn14.OptionsColumn.ReadOnly = True
            ' Me.GridColumn14.Visible = True
            Me.GridColumn15.FieldName = "Item_Model"
            Me.GridColumn15.Caption = "รุ่น (Model)"
            Me.GridColumn15.OptionsColumn.ReadOnly = True
            ' Me.GridColumn15.Visible = False
            Me.GridColumn16.FieldName = "Item_From_Company"
            Me.GridColumn16.Caption = "ซื้อจากบริษัท"
            Me.GridColumn16.OptionsColumn.ReadOnly = True
            Me.GridColumn16.Visible = False
            Me.GridColumn17.FieldName = "serialno"
            Me.GridColumn17.Caption = "รหัสผลิตภัณฑ์"
            Me.GridColumn17.OptionsColumn.ReadOnly = True
            Me.GridColumn18.FieldName = "Item_EngName"
            Me.GridColumn18.Caption = "ชื่อครุภัณฑ์ (ภาษาอังกฤษ)"
            Me.GridColumn18.OptionsColumn.ReadOnly = True
            Me.GridColumn3.FieldName = "Budget_year"
            Me.GridColumn3.Caption = "ปีงบประมาณ"
            Me.GridColumn3.OptionsColumn.ReadOnly = True
            Me.GridColumn4.FieldName = "Budget_Name"
            Me.GridColumn4.Caption = "ได้รับจากงบ"
            Me.GridColumn4.OptionsColumn.ReadOnly = True
            Me.GridColumn5.FieldName = "Item_Price"
            Me.GridColumn5.Caption = "ราคาต่อหน่วย"
            Me.GridColumn5.OptionsColumn.ReadOnly = True
            Me.GridColumn6.FieldName = "Receive_Date"
            Me.GridColumn6.Caption = "วันที่ได้รับ"
            Me.GridColumn6.OptionsColumn.ReadOnly = True
            Me.GridColumn7.FieldName = "Department_Name"
            Me.GridColumn7.Caption = "แผนก/ฝ่าย"
            Me.GridColumn7.OptionsColumn.ReadOnly = True
            Me.GridColumn8.FieldName = "Ref_DocReceiveID"
            Me.GridColumn8.Caption = "เลขที่เอกสารับ"
            Me.GridColumn8.OptionsColumn.ReadOnly = True
            Me.GridColumn9.FieldName = "Ref_DocDrawID"
            Me.GridColumn9.Caption = "เลขที่เอกสารเบิก"
            Me.GridColumn9.OptionsColumn.ReadOnly = True
            Me.GridColumn10.FieldName = "Discharge"
            Me.GridColumn10.Caption = "จำหน่าย"
            Me.GridColumn10.OptionsColumn.ReadOnly = True
            Me.GridColumn1.Caption = "Item_ID"
            Me.GridColumn1.FieldName = "Item_ID"
            With GridItem
                Strsql = "Select Item_ID,Item_Type,Item_Code,Concat(Item_Type,Item_Code) as fullItemcode,Item_Name,Budget_year,Budget_Name,Item_Price,Receive_Date,Department_Name,Item_From_Company,Product_From_Country,Item_Model,Band,serialno,Item_EngName,"
                Strsql = Strsql & " Ref_DocReceiveID,Ref_DocDrawID,Discharge,Band,Item_EngName,Product_From_Country,Item_From_Company,Item_Desc,Item_Model"
                Strsql = Strsql & ",'' as companylist,'' as price_from_companylist,item_status,Status_Name "
                Strsql = Strsql & " from items "
                Strsql = Strsql & " left join budget on items.Item_Budget = budget.Budget_ID "
                Strsql = Strsql & " left join department on items.department = department.department_id "
                Strsql = Strsql & " left join typeitem on items.Item_Type = typeitem.Type_ID "
                Strsql = Strsql & " left join itemstatus on items.item_status = itemstatus.Status_ID "
                Strsql = Strsql & " where Item_Code = '' order by Item_Type"
            End With
            Dim DT_Items As New DataTable
            DT_Items = _Mysql.GetMYSQLDataTable(Strsql, "Items")
            GridItem.DataSource = DT_Items
            If DT_Items.Rows.Count <> 0 Then
                Primary_Code = DT_Items.Rows(0).Item("Item_ID")
            End If
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Load_Data"
                .MemoErr_Description.Text = "SQL Error กรุณาตรวจสอบคำสั่ง"
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try



    End Sub
#End Region
    Private Sub frmWaitCodeItems_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        
        Load_Data()
    End Sub

    Private Sub BtnExcelExport_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BtnExcelExport.ItemClick
        If (MsgBox("คุณต้องการส่งออกข้อมูลเป็น Excel ใช่หรือไม่", MsgBoxStyle.Question + MsgBoxStyle.YesNo)) = MsgBoxResult.No Then Exit Sub

        With SaveFileDialog1
            .Title = "ส่งออกข้อมูล"
            .FileName = "ข้อมูลสำรวจครุภัณฑ์.xls"
            .Filter = "Excel files (*.xls)|*.xls|" & "All files|*.*"
            .ShowDialog()
        End With

        GridItem.ExportToXls(SaveFileDialog1.FileName.ToString)
    End Sub

    Private Sub BtnRefresh_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BtnRefresh.ItemClick
        Load_Data()
    End Sub

   

    Private Sub BtnEdit_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BtnEdit.ItemClick
        With FrmEditItem


            If Primary_Code = -1 Then Exit Sub
           
            .Primary_Code = Me.Primary_Code
            If .ShowDialog() = Windows.Forms.DialogResult.Cancel Then Exit Sub
            Load_Data()

        End With
    End Sub

    Private Sub BtnDelete_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BtnDelete.ItemClick
        If (MsgBox("กรุณายืนยันการลบข้อมูล", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "ยืนยันการลบข้อมูล") = MsgBoxResult.No) Then Exit Sub
        If (MsgBox("การลบข้อมูลเมื่อยืนยันแล้วจะไม่สามารถนำกลับมาใหม่ได้ คุณยังต้องการยืนยันใช่หรือไม่", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "ยืนยันการลบข้อมูล") = MsgBoxResult.No) Then Exit Sub

        Try
            'Delete Item Information
            ' Strsql = "Delete from items where concat(item_type,item_code)='" & Me.TxtCode.Text.ToString.Trim & "'"
            Strsql = "Delete from items where item_id='" & Me.Primary_Code & "'"
            If _Mysql.MySQLExecute(Strsql) = -1 Then
                frmDebug.MemoErr_Description.Text = "SQL Error กรุณาตรวจสอบคำสั่ง"
                frmDebug.MemoSQL.Text = Strsql
                frmDebug.ShowDialog()
                Exit Sub
            Else

                MsgBox("ลบข้อมูลเรียบร้อยแล้ว", MsgBoxStyle.Information, "ผลการลบข้อมูล")

                Load_Data()
            End If

            'Add Log
            Strsql = "Insert into transections_logs (trans_code,trans_datetime,trans_computer,trans_item) values (3,'" & Pn_Framework.MysqlDateTimeFormat(Now, True) & "','" & Pn_Framework.Get_Computername & "','" & Me.Primary_Code & "') "
            Select Case (_Mysql.MySQLExecute(Strsql))
                Case -1
                    frmDebug.lblFormName.Text = Me.Name
                    frmDebug.lblFunctionName.Text = "BtnDelete_ItemClick"
                    frmDebug.MemoErr_Description.Text = "SQL Error กรุณาตรวจสอบคำสั่ง"
                    frmDebug.MemoSQL.Text = Strsql
                    frmDebug.ShowDialog()
            End Select
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub GridView1_CustomRowCellEditForEditing(ByVal sender As System.Object, ByVal e As DevExpress.XtraGrid.Views.Grid.CustomRowCellEditEventArgs) Handles GridView1.CustomRowCellEditForEditing
        Try

            Dim Gv As GridView = sender


            Primary_Code = Gv.GetRowCellValue(e.RowHandle, Gv.Columns("Item_ID")).ToString()

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
End Class